package com.weipai.education.service.impl;

import com.weipai.education.dao.ActivityDao;
import com.weipai.education.dao.UserDao;
import com.weipai.education.enums.RetCode;
import com.weipai.education.enums.UserType;
import com.weipai.education.model.Activity;
import com.weipai.education.model.User;
import com.weipai.education.model.api.PageResult;
import com.weipai.education.model.api.Response;
import com.weipai.education.service.ActivityService;
import com.weipai.education.utils.SessionUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/11 10:30
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    
    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private SessionUserUtil sessionUserUtil;
    @Autowired
    private UserDao userDao;

    @Override
    public Response list(Integer page, Integer size, Integer isAudit, Integer activityCateId, String key, String type) {
        Response response = new Response();
        String userID = null;

        //从session中获取userId
        userID = sessionUserUtil.getUser().getId().toString();
        User user = userDao.selectByPrimaryKey(userID);
        if(user.getType()== UserType.HQ_USER.getType()){

            if(null == type){

                //从session中获取userId
                userID = null;

            }

            Integer start = (page-1) * size;
            List<Map<String, Object>> activity = activityDao.list(start, size, isAudit, activityCateId, key, userID);
            Integer totalNum = activityDao.listAmount(isAudit, activityCateId, key, userID);
            PageResult<Map<String, Object>> pageList = new PageResult<>(activity,page,size,totalNum);
            response.setCode(RetCode.SUCCESS);
            response.put("pageList", pageList);
            return response;
        }else {
            if (user.getType()==UserType.SELL_USER.getType()){

                Integer start = (page-1) * size;
                 //查询user的活动
               /* Activity activity=activityDao.selectByPrimaryKey(userID);
                Integer totalNum = activityDao.listAmount(isAudit, activityCateId, key, userID);
                PageResult<Map<String, Object>> pageList = new PageResult((List) activity,page,size,totalNum);*/

                List<Map<String, Object>> activity = activityDao.list(start, size, isAudit, activityCateId, key, userID);

                Integer totalNum = activityDao.listAmount(isAudit, activityCateId, key, userID);
                if (totalNum==0){
                    response.setCode(RetCode.No_ACTIVITY);
                    return response;
                }

                PageResult<Map<String, Object>> pageList = new PageResult<>(activity,page,size,totalNum);
                response.setCode(RetCode.SUCCESS);
                response.put("pageList",pageList);
                return response;

            }
        }
           // response.setCode(RetCode.No_ACTIVITY);
            return response;




        /*Integer start = (page-1) * size;
        List<Map<String, Object>> activity = activityDao.list(start, size, isAudit, activityCateId, key, userID);
        Integer totalNum = activityDao.listAmount(isAudit, activityCateId, key, userID);
        PageResult<Map<String, Object>> pageList = new PageResult<>(activity,page,size,totalNum);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;*/

    }

    @Override
    public Response save(Activity activity) {
        Response response = new Response();
        if(null !=activity.getId()){
            activityDao.update(activity);
        }else{
            String userId = sessionUserUtil.getUser().getId().toString();
            activity.setUserId(Integer.parseInt(userId));
            activity.setIsAudit(0);
            activityDao.insert(activity);
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response isAudit(String id, String isAudit) {
        Response response = new Response();
        Activity activity = activityDao.selectByPrimaryKey(id);
        if(null!=activity){
            if (!activity.getIsAudit().equals(isAudit)){
                Integer userId = sessionUserUtil.getUser().getId();
                activity.setIsAudit(Integer.parseInt(isAudit));
                activity.setAuditorId(userId);
                activityDao.update(activity);
            }
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.NOT_DATA);
        }
        return response;
    }

    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        if(activityDao.batchDeleteById(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }
}
